home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / WINPROGS / UPC12BS1.ZIP / LIB / SECURITY.C < prev    next >
C/C++ Source or Header  |  1993-10-03  |  34KB  |  875 lines

  1. /*--------------------------------------------------------------------*/
  2. /*    s e c u r i t y . c                                             */
  3. /*                                                                    */
  4. /*    Security routines for UUPC/extended                             */
  5. /*                                                                    */
  6. /*    Copyright (c) 1991, Andrew H. Derbyshire                        */
  7. /*    See README.PRN for additional copyrights and restrictions       */
  8. /*--------------------------------------------------------------------*/
  9.  
  10. /*--------------------------------------------------------------------*/
  11. /*                          RCS Information                           */
  12. /*--------------------------------------------------------------------*/
  13.  
  14. /*
  15.  *    $Id: security.c 1.9 1993/10/03 20:37:34 ahd Exp $
  16.  *
  17.  *    Revision history:
  18.  *    $Log: security.c $
  19.  *     Revision 1.9  1993/10/03  20:37:34  ahd
  20.  *     Lower case all strings loaded into directory array
  21.  *
  22.  *     Revision 1.8  1993/09/20  04:38:11  ahd
  23.  *     TCP/IP support from Dave Watt
  24.  *     't' protocol support
  25.  *     OS/2 2.x support
  26.  *
  27.  *     Revision 1.7  1993/08/03  03:11:49  ahd
  28.  *     Make missing directories non-fatal
  29.  *
  30.  *     Revision 1.6  1993/05/06  03:41:48  ahd
  31.  *     Use NULL to denote current directory, not "."
  32.  *
  33.  *     Revision 1.5  1993/04/11  00:31:04  ahd
  34.  *     Global edits for year, TEXT, etc.
  35.  *
  36.  *     Revision 1.4  1993/03/06  22:48:23  ahd
  37.  *     Re-do compare of sort to void bug in some qsort() functions
  38.  *
  39.  * Revision 1.3  1992/11/22  20:58:55  ahd
  40.  * Normalize directories as read
  41.  * Use strpool to allocate const strings
  42.  *
  43.  * Revision 1.2  1992/11/19  02:57:31  ahd
  44.  * drop rcsid
  45.  *
  46.  * Revision 1.1  1992/11/16  05:00:26  ahd
  47.  * Initial revision
  48.  *
  49.  */
  50.  
  51. /*--------------------------------------------------------------------*/
  52. /*                        System include files                        */
  53. /*--------------------------------------------------------------------*/
  54.  
  55. #include <stdio.h>
  56. #include <stdlib.h>
  57. #include <ctype.h>
  58. #include <string.h>
  59. #include <sys/types.h>        /* Only really needed for MS C         */
  60. #include <sys/stat.h>
  61. #include <time.h>
  62. #include <direct.h>
  63.  
  64. /*--------------------------------------------------------------------*/
  65. /*                    UUPC/extended include files                     */
  66. /*--------------------------------------------------------------------*/
  67.  
  68. #include "lib.h"
  69. #include "hostable.h"
  70. #include "security.h"
  71. #include "usertabl.h"
  72. #include "expath.h"
  73. #include "hlib.h"
  74.  
  75. /*--------------------------------------------------------------------*/
  76. /*                           Local defines                            */
  77. /*--------------------------------------------------------------------*/
  78.  
  79. static boolean InitEntry( char *buf, const char *fname);
  80.  
  81. static size_t InitDir( char *directories,
  82.          const REMOTE_ACCESS access,
  83.          const boolean grant,
  84.          struct HostSecurity *anchor,
  85.          size_t max_elements );
  86.  
  87. int dircmp( const void *a , const void *b );
  88.  
  89. /*--------------------------------------------------------------------*/
  90. /*                          Global varables                           */
  91. /*--------------------------------------------------------------------*/
  92.  
  93. struct HostSecurity *securep = NULL;
  94. static struct HostSecurity *default_security = NULL;
  95. static char drive[] = "C:";
  96.  
  97. currentfile();
  98.  
  99. /*--------------------------------------------------------------------*/
  100. /*    L o a d S e c u r i t y                                         */
  101. /*                                                                    */
  102. /*    Initialize security processing; returns TRUE if security        */
  103. /*    initialized, otherewise FALSE                                   */
  104. /*--------------------------------------------------------------------*/
  105.  
  106. boolean LoadSecurity( void )
  107. {
  108.    char fname[FILENAME_MAX];
  109.    char buffer[BUFSIZ*4];     /* Allows around 2K for the data       */
  110.    struct HostTable *hostp;
  111.    FILE *stream;
  112.  
  113. /*--------------------------------------------------------------------*/
  114. /*      Generate a filename for the permissions file and open it      */
  115. /*--------------------------------------------------------------------*/
  116.  
  117.    mkfilename(fname, E_confdir, PERMISSIONS);
  118.    stream  = FOPEN( fname, "r",TEXT_MODE);
  119.  
  120.    if ( stream == NULL )      /* Did the file open?                  */
  121.    {                          /* No --> Report failure to caller     */
  122.       printerr( fname );
  123.       return FALSE;
  124.    } /* ( stream == NULL ) */
  125.  
  126. /*--------------------------------------------------------------------*/
  127. /*              Get current drive for normalizing names               */
  128. /*--------------------------------------------------------------------*/
  129.  
  130.    getcwd( buffer, sizeof buffer );
  131.    *drive = *buffer;
  132.  
  133. /*--------------------------------------------------------------------*/
  134. /*               Begin processing the PERMISSIONS file                */
  135. /*--------------------------------------------------------------------*/
  136.  
  137.    while ( !feof( stream ) )
  138.    {
  139.       char *next = buffer;
  140.  
  141. /*--------------------------------------------------------------------*/
  142. /*                Build up the buffer to be processed                 */
  143. /*--------------------------------------------------------------------*/
  144.  
  145.       *next = '\0';
  146.       while( fgets( next, sizeof buffer - strlen(next), stream ) != NULL)
  147.       {
  148.          if ((*next == '#') || (*next == '\n'))
  149.          {
  150.             *next = '\0';
  151.             continue;
  152.          }
  153.  
  154.          next = next + strlen( next ) - 1;
  155.          if (*next == '\n')
  156.             *next-- = '\0';
  157.          else if (!feof( stream ))  /* Did we hit EOF?               */
  158.          {                    /* No --> Presume the buffer overflowed*/
  159.             printmsg(0,"LoadSecurity: buffer overflow while reading %s",
  160.              fname);
  161.             fclose( stream );
  162.             return FALSE;
  163.          }
  164.  
  165.          while( isspace( *next ))   /* Dump trailing white space     */
  166.             *next-- = '\0';
  167.  
  168.          if (*next == '\\')
  169.             *next = '\0';
  170.          else
  171.             break;
  172.       } /* while( fgets( next, sizeof available, stream )) != NULL)) */
  173.  
  174. /*--------------------------------------------------------------------*/
  175. /*            Done read the data; verify we had no errors             */
  176. /*--------------------------------------------------------------------*/
  177.  
  178.       if (ferror( stream ))
  179.       {
  180.          printerr( fname );
  181.          clearerr( stream );
  182.          return FALSE;
  183.       } /* if */
  184.  
  185. /*--------------------------------------------------------------------*/
  186. /*              Build entries for one permissions entry               */
  187. /*--------------------------------------------------------------------*/
  188.  
  189.       printmsg(10,"Buffer is \"%s\"", buffer );
  190.       if ((*next != '\0') && !InitEntry( buffer , fname))
  191.       {
  192.          fclose( stream );
  193.          return FALSE;
  194.       }
  195.  
  196.    } /* while ( !feof( stream ) ) */
  197.  
  198. /*--------------------------------------------------------------------*/
  199. /*                    Initialize local host entry                     */
  200. /*--------------------------------------------------------------------*/
  201.  
  202.    hostp = checkname( E_nodename );
  203.    if ( hostp == NULL )
  204.       panic();
  205.    hostp->hsecure = malloc( sizeof *hostp->hsecure );
  206.    checkref( hostp->hsecure );
  207.    memset( hostp->hsecure , '\0', sizeof *hostp->hsecure);
  208.                               /* Clear pointers                      */
  209.    hostp->hsecure->local = TRUE;
  210.  
  211. /*--------------------------------------------------------------------*/
  212. /*                          Return to caller                          */
  213. /*--------------------------------------------------------------------*/
  214.  
  215.    fclose( stream );
  216.    return TRUE;
  217.  
  218. } /* LoadSecurity */
  219.  
  220. /*--------------------------------------------------------------------*/
  221. /*    I n i t i a l i z e